<?php

class View_Helper_HelperFecha extends Model3_View_Helper
{

    public function formatoFecha($fecha, $separator = '/')
    {
        $dateTime = strtotime($fecha);

        if($fecha == date('Y-m-d'))
        {
            return 'Hoy';
        }
        switch(date('w', $dateTime))
        {
            case 0:
                $strDate = 'Domingo, ';
                break;
            case 1:
                $strDate = 'Lunes, ';
                break;
            case 2:
                $strDate = 'Martes, ';
                break;
            case 3:
                $strDate = 'Miercoles, ';
                break;
            case 4:
                $strDate = 'Jueves, ';
                break;
            case 5:
                $strDate = 'Viernes, ';
                break;
            case 6:
                $strDate = 'Sabado, ';
                break;
        }
        $strDate .= date('d', $dateTime).$separator;
        switch(date('m', $dateTime))
        {
            case '01':
                $strDate .= 'Ene';
                break;

            case '02':
                $strDate .= 'Feb';
                break;

            case '03':
                $strDate .= 'Mar';
                break;

            case '04':
                $strDate .= 'Abr';
                break;

            case '05':
                $strDate .= 'May';
                break;

            case '06':
                $strDate .= 'Jun';
                break;

            case '07':
                $strDate .= 'Jul';
                break;

            case '08':
                $strDate .= 'Ago';
                break;

            case '09':
                $strDate .= 'Sep';
                break;

            case '10':
                $strDate .= 'Oct';
                break;

            case '11':
                $strDate .= 'Nov';
                break;

            case '12':
                $strDate .= 'Dic';
                break;
        }
        $strDate .= $separator.date('Y', $dateTime);
        return $strDate;
    }
    
    public function humanFormat($date)
    {
        $str = '';
        try
        {
            $date = new Class_DateTimeSpanish($date);
            $str = $date->humanFormat();
        }
        catch(Exception $e)
        {
            
        }
        return $str;
    }

    public function getFullMonth($mes)
    {
        $mesStr = '';
        switch($mes)
        {
            case 1:
            case '01':
                $mesStr = 'Enero';
                break;
            case 2:
            case '02':
                $mesStr = 'Febrero';
                break;
            case 3:
            case '03':
                $mesStr = 'Marzo';
                break;
            case 4:
            case '04':
                $mesStr = 'Abril';
                break;
            case 5:
            case '05':
                $mesStr = 'Mayo';
                break;
            case 6:
            case '06':
                $mesStr = 'Junio';
                break;
            case 7:
            case '07':
                $mesStr = 'Julio';
                break;
            case 8:
            case '08':
                $mesStr = 'Agosto';
                break;
            case 9:
            case '09':
                $mesStr = 'Septiembre';
                break;
            case 10:
            case '10':
                $mesStr = 'Octubre';
                break;
            case 11:
            case '11':
                $mesStr = 'Noviembre';
                break;
            case 12:
            case '12':
                $mesStr = 'Diciembre';
                break;
        }
        return $mesStr;
    }
    
    public function getShortMonth($month)
    {
        $strDate = '';
        switch($month)
        {
            case '01':
                $strDate .= 'Ene';
                break;

            case '02':
                $strDate .= 'Feb';
                break;

            case '03':
                $strDate .= 'Mar';
                break;

            case '04':
                $strDate .= 'Abr';
                break;

            case '05':
                $strDate .= 'May';
                break;

            case '06':
                $strDate .= 'Jun';
                break;

            case '07':
                $strDate .= 'Jul';
                break;

            case '08':
                $strDate .= 'Ago';
                break;

            case '09':
                $strDate .= 'Sep';
                break;

            case '10':
                $strDate .= 'Oct';
                break;

            case '11':
                $strDate .= 'Nov';
                break;

            case '12':
                $strDate .= 'Dic';
                break;
        }
        return $strDate;
    }

    public function mesAnio(DateTime $date)
    {
        $mes = $date->format('m');
        $strPeriodo = $this->getFullMonth($mes).' de '.$date->format('Y');
        return $strPeriodo;
    }

    public function numberMonth($monthString)
    {
        $month = 1;
        switch($monthString)
        {
            case 'Enero':
                $moth = 1;
                break;
            case 'Febrero':
                $moth = 2;
                break;
            case 'Marzo':
                $moth = 3;
                break;
            case 'Abril':
                $moth = 4;
                break;
            case 'Mayo':
                $moth = 5;
                break;
            case 'Junio':
                $moth = 6;
                break;
            case 'Julio':
                $moth = 7;
                break;
            case 'Agosto':
                $moth = 8;
                break;
            case 'Septiembre':
                $moth = 9;
                break;
            case 'Octubre':
                $moth = 10;
                break;
            case 'Noviembre':
                $moth = 11;
                break;
            case 'Diciembre':
                $moth = 12;
                break;
        }
        return $moth;
    }

    public function getPayDate($day)
    {
        $fechaActual = new DateTime(); // $date('Y-m-d');
        $fechaDeCorteTentativo = new DateTime(date('Y-m-'.$day));

        if($fechaDeCorteTentativo == $fechaActual)
        {
            $fechaDePago = 'Hoy ('.$fechaActual->format('Y-m-d').')';
        }
        else
        {
            if($fechaDeCorteTentativo > $fechaActual)
            {
                $fechaDePago = $fechaDeCorteTentativo->format('Y-m-d');
            }
            else
            {
                $fechaDePago = $fechaDeCorteTentativo->modify('+1 month')->format('Y-m-d');
            }
            $fechaDePago = $this->formatoFecha($fechaDePago);
        }

        return $fechaDePago;
    }

    /**
     * Metodo que crea dos combobox para imprimir horas y minutos respectivamente.
     * @param string $id
     * @param integer $defHour
     * @param integer $defMin
     * @return string
     */
    public function drawTimeSelects($id, $defHour=0, $defMin=0)
    {
        $string = '<select id="'.$id.'_hours" name="'.$id.'_hours" >';

        $i = 0;
        for($i; $i < 24; $i++)
        {
            $msg = $i == $defHour ? 'selected' : '';
            $string.= '<option value="'.$i.'" '.$msg.' >'.$i.'</option>';
        }
        $string .= '</select> <select id="'.$id.'_minutes" name="'.$id.'_minutes"> ';

        $i = 0;
        for($i; $i < 60; $i++)
        {
            $msg = $i == $defMin ? 'selected' : '';
            $string.= '<option value="'.$i.'" '.$msg.' >'.$i.'</option>';
        }

        $string .= '</select>';

        return $string;
    }

    public function fullFormatSmartSociety($fecha)
    {
        $dateTime = strtotime($fecha);
        $dayOfWeek = date('w', $dateTime);
        $day = date('d', $dateTime);
        $month = date('m', $dateTime);
        $year = date('Y', $dateTime);
        $time = date('H:i', $dateTime);
        $strDayOfWeek = $this->getNameDay($dayOfWeek);
        $strMonthName = $this->getFullMonth($month);
        $strDate = $strDayOfWeek.', '.$day.' de '.$strMonthName.' de '.$year.' a las '.$time;
        return $strDate;
    }

    public function smallFormatSmartSociety($fecha)
    {
        $dateTime = strtotime($fecha);
        $dayOfWeek = date('w', $dateTime);
        $objDate = new DateTime($fecha);
        $objCurrentDate = new DateTime();
        $ayerDate = new DateTime();
        $ayerDate->modify('-1 Day');
        if($objDate->format('Y-m-d') == $objCurrentDate->format('Y-m-d'))
        {
            $strDayOfWeek = 'Hoy';
        }
        else if($objDate->format('Y-m-d') == $ayerDate->format('Y-m-d'))
        {
            $strDayOfWeek = 'Ayer';
        }
        else
        {
            $day = date('d', $dateTime);
            $month = date('m', $dateTime);
            $strMonthName = $this->getFullMonth($month);
            $strDayOfWeek = $this->getNameDay($dayOfWeek).', '.$day.' de '.$strMonthName;
        }
        $time = $objDate->format('H:i');
        $strDate = $strDayOfWeek.' a las '.$time;
        return $strDate;
    }

    private function getNameDay($day)
    {
        $strDate = '';
        switch($day)
        {
            case 0:
                $strDate = 'Domingo';
                break;
            case 1:
                $strDate = 'Lunes';
                break;
            case 2:
                $strDate = 'Martes';
                break;
            case 3:
                $strDate = 'Miercoles';
                break;
            case 4:
                $strDate = 'Jueves';
                break;
            case 5:
                $strDate = 'Viernes';
                break;
            case 6:
                $strDate = 'Sabado';
                break;
        }
        return $strDate;
    }
    
    public function dateWall($date)
    {
        $obj = new DateTime($date);
        $dayOfWeek = $obj->format('w');
        $month = $obj->format('m');
        $strDate = $this->getNameDay($dayOfWeek).', '.$obj->format('Y/').$this->getShortMonth($month).$obj->format('/d');
        return $strDate;
    }

}